package person.bluext.singlegateway.frame.utils;


import person.bluext.singlegateway.common.constants.ProjectConstants;
import person.bluext.singlegateway.common.constants.SystemConstants;
import person.bluext.singlegateway.frame.model.HttpInfo;
import person.bluext.singlegateway.frame.model.Response;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Description : Utils to generate request and response log
 *
 * @author : bluext
 */
public class LogUtils {

    /**
     * 组装请求参数信息
     *
     * @param request    请求对象
     * @param traceId    追踪ID
     * @param jsonReqStr json格式请求值
     * @return 请求信息
     */
    public static String assembleReqInfo(HttpServletRequest request, String traceId, String jsonReqStr) throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        sb.append(SystemConstants.LINE_SEPARATOR).append(SystemConstants.LINE_SEPARATOR);
        sb.append("=== Request ================================").append(SystemConstants.LINE_SEPARATOR);
        sb.append("TraceID: ").append(traceId).append(SystemConstants.LINE_SEPARATOR);
        sb.append("RequestURL: ").append(request.getRequestURL()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Encoding: ").append(request.getCharacterEncoding()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Method: ").append(request.getMethod()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Content-Type: ").append(request.getContentType()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Headers: ").append(HttpInfoUtils.getStringHeaders(request)).append(SystemConstants.LINE_SEPARATOR);
        HttpInfo reqIpInfo = HttpInfoUtils.getReqIpInfo(request);
        if (reqIpInfo.isDispatched()) {
            sb.append("ClientIP: ").append(reqIpInfo.getX_real_ip()).append(SystemConstants.LINE_SEPARATOR);
        } else {
            sb.append("ClientIP: ").append(reqIpInfo.getRemoteAddress()).append(SystemConstants.LINE_SEPARATOR);
        }
        sb.append("RequestPayload: ").append(URLDecoder.decode(jsonReqStr, "UTF-8")).append(SystemConstants.LINE_SEPARATOR);
        sb.append("============================================").append(SystemConstants.LINE_SEPARATOR).append(SystemConstants.LINE_SEPARATOR);
        return sb.toString();
    }

    /**
     * 组装响应参数信息
     *
     * @param response    响应对象
     * @param traceId     追踪ID
     * @param resp        响应结果对象
     * @param beginMillis 请求开始时间
     * @return 响应信息
     */
    public static String assembleRespInfo(HttpServletResponse response, String traceId, Response resp, Long beginMillis) {
        StringBuilder sb = new StringBuilder();
        sb.append(SystemConstants.LINE_SEPARATOR).append(SystemConstants.LINE_SEPARATOR);
        sb.append("=== Response ===============================").append(SystemConstants.LINE_SEPARATOR);
        sb.append("TraceID: ").append(traceId).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Status: ").append(response.getStatus()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Encoding: ").append(response.getCharacterEncoding()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Content-Type: ").append(response.getContentType()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Headers: ").append(HttpInfoUtils.getStringHeaders(response)).append(SystemConstants.LINE_SEPARATOR);
        sb.append("Response: ").append(resp.toJsonStr()).append(SystemConstants.LINE_SEPARATOR);
        sb.append("CostTime: ").append(System.currentTimeMillis() - beginMillis).append(ProjectConstants.MILLISECOND).append(SystemConstants.LINE_SEPARATOR);
        sb.append("============================================").append(SystemConstants.LINE_SEPARATOR).append(SystemConstants.LINE_SEPARATOR);
        return sb.toString();
    }
}
